package com.spring.aop.dynproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理：工厂类写法
 *
 * 1、先有代理对象 ：Proxy
 * 2、干活
 */
public class JDKProxyFactory implements InvocationHandler{

    private Object target;

    public JDKProxyFactory(Object obj) {
        this.target = obj;
    }

    public  Object getProxy(){
        Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return  proxy;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("汽车经销商：性价比很高！");
        Object result = method.invoke(target, args);  //干活
        System.out.println("汽车经销商：赚差价！");
        return result;
    }

}
